package com.zcl.lambda.test1;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * java内置的4大核心函数式接口
 *
 *  消费型接口 Consumer<T>      void accept(T t)
 *  供给型接口 supplier<T>      T get()
 *  函数型接口 Function<T, R>   R appLy(T t)
 *  断定型接口 predicate<T>     boolean test(T t)

 */
public class functionTest {

    @Test
    public void test1(){
        happyTime(500.0, new Consumer<Double>() {
            @Override
            public void accept(Double aDouble) {
                System.out.println(aDouble);
            }
        });


        //使用lambda
        happyTime(400.0, a -> System.out.println(a));
    }

    public void happyTime(Double money, Consumer<Double> con) {
        con.accept(money);
    }




    @Test
    public void test2() {
        List<String> list = Arrays.asList("北京”，“南京”，“天津", "东京", "西京", "普京");
        List<String> filterStrs = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        System.out.println(filterStrs);

        //lambda表达式写法
        List<String> filterStrs2 = filterString(list, s-> s.contains("京"));
        System.out.println(filterStrs2);
    }

    //根据给定的规则，过滤集合中的字符串。此规则bpredicate 的方法决定
    public List<String> filterString(List<String> list, Predicate<String> pre) {
        ArrayList<String> filterList = new ArrayList<>();
        for (String s : list) {
            if (pre.test(s)) {
                filterList.add(s);
            }
        }
        return filterList;
    }
}